package cn.o.app.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

@SuppressWarnings("unchecked")
public class ODialog extends Dialog implements IViewFinder {

	public ODialog(Context context) {
		super(context);
		init();
	}

	public ODialog(Context context, int theme) {
		super(context, theme);
		init();
	}

	public ODialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init();
	}

	protected void init() {
		this.setCancelable(true);
		this.setCanceledOnTouchOutside(true);
		Window w = getWindow();
		w.requestFeature(Window.FEATURE_NO_TITLE);
		w.setBackgroundDrawable(new ColorDrawable(0x00000000));
	}

	/**
	 * fix bug of default Dialog width not full screen. please call before show
	 * and after setContentView if you want to make width MATCH_PARENT
	 */
	public void requestLayout() {
		Window w = getWindow();
		WindowManager.LayoutParams attrs = w.getAttributes();
		w.setLayout(WindowManager.LayoutParams.MATCH_PARENT, attrs.height);
	}

	public void setWindowAnimations(int resId) {
		Window w = getWindow();
		w.setWindowAnimations(resId);
	}

	public <T extends View> T findViewById(int id, Class<T> viewClass) {
		View v = findViewById(id);
		if (v != null) {
			if (viewClass != null) {
				if (viewClass.isInstance(v)) {
					return (T) v;
				} else {
					return null;
				}
			}
		}
		return null;
	}

}
